﻿using System.Data;
using System.Web.UI.WebControls;
using System.Text;

namespace Utils
{
  public   class PrintUtility
    {

        /// <summary>
        /// 拼出要打印的表
        /// </summary>
        /// <param name="ds">DATASET</param>
        /// <param name="dg">DATAGRID</param>
        /// <returns></returns>
      public string DTPrint(DataTable myDataTable)
        {
            //*************************************************************//
          

            int myRow = myDataTable.Rows.Count;
            int myCol = myDataTable.Columns.Count;

            StringBuilder sb = new StringBuilder();

            string colHeaders = "<html><body>" +
                "<object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'VIEWASTEXT></object>"
                + "<table><tr>";

            for (int i = 0; i < myCol; i++)
            {
                colHeaders += "<td>" + myDataTable.Columns[i].ColumnName.ToString() + "</td>";
            }
            colHeaders += "</tr>";
            sb.Append(colHeaders);

            for (int i = 0; i < myRow; i++)
            {
                sb.Append("<tr>");
                for (int j = 0; j < myCol; j++)
                {
                    sb.Append("<td>");
                    sb.Append(myDataTable.Rows[i][j].ToString().Trim());
                    sb.Append("</td>");
                }
                sb.Append("</tr>");
            }

            sb.Append("</table></body></html>");
            colHeaders = sb.ToString();
            colHeaders += "<script languge='javascript'>WebBrowser.ExecWB(6,1); window.opener=null;window.close();</script>";
            return (colHeaders);
            //*************************************************************//

        }

        /// <summary>
        ///  拼出要打印的表
        /// </summary>
        /// <param name="gv">GridView</param>
        /// <returns></returns>
        public string GVPrint(GridView gv)
        {
            int myRow = gv.Rows.Count;
            int myCol = gv.Columns.Count;
            StringBuilder sb = new StringBuilder();

            string colHeaders = "<html><body>" +
                "<object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'VIEWASTEXT></object>"
                + "<table><tr>";

            for (int i = 0; i < myCol; i++)
            {
                colHeaders += "<td>" + gv.Columns[i].HeaderText.ToString() + "</td>";
            }
            colHeaders += "</tr>";
            sb.Append(colHeaders);

            for (int i = 0; i < myRow; i++)
            {
                sb.Append("<tr>");
                for (int j = 0; j < myCol; j++)
                {
                    sb.Append("<td>");
                    sb.Append(gv.Rows[i].Cells[j].Text.Trim());
                    sb.Append("</td>");
                }
                sb.Append("</tr>");
            }
            sb.Append("</table></body></html>");
            colHeaders = sb.ToString();
            colHeaders += "<script languge='javascript'>WebBrowser.ExecWB(7,1); window.opener=null;window.close();</script>";
            return (colHeaders);
        }

    }
}
